package ar.edu.uba.fi.jmit.graphvisualizer.receiver;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import ar.edu.uba.fi.jmit.graphvisualizer.server.ResourceProperties;
import ar.edu.uba.fi.jmit.integrationcommons.Event;
import ar.edu.uba.fi.jmit.receiver.ReceiverObserver;

/**
 * 
 * TODO TEST!
 *
 */
public class DynamicMenuObserverImpl implements ReceiverObserver {

	private Map<String, List<String>> applicationMonitors;
	private Map<String, Map<String, Serializable>> mapDatas;
	private static DynamicMenuObserverImpl instance = null;
	
	private DynamicMenuObserverImpl() {
		applicationMonitors = new HashMap<String, List<String>>();
		mapDatas = new HashMap<String, Map<String,Serializable>>();
	}
	
	public static DynamicMenuObserverImpl getInstance() {
		if (instance == null)
			instance = new DynamicMenuObserverImpl();
		return instance;
	}
	
	@Override
	public void onRecivedEvent(Event event) {
		if(event.getData().get(ResourceProperties.getProperty("jmit.graphvisualizer.type")).equals("TYPE_RANGE")) {
			//dont need the ranges events here
			return;
		}
		String application = (String)event.getData().get(ResourceProperties.getProperty("jmit.graphvisualizer.monitor.clientApplicationName"));
		String monitor = (String)event.getData().get(ResourceProperties.getProperty("jmit.graphvisualizer.key.monitorName"));
		
		if (!applicationMonitors.containsKey(application))
			applicationMonitors.put(application, new LinkedList<String>());
		if (!applicationMonitors.get(application).contains(monitor)) {
			applicationMonitors.get(application).add(monitor);
			mapDatas.put(createMapKey(application, monitor), event.getData());
		}
	}

	private String createMapKey(String application, String monitor) {
		return application + "-" + monitor;
	}
	
	public Map<String, List<String>> getApplicationMonitors() {
		return applicationMonitors;
	}
	
	public Map<String, Serializable> getData(String appName, String monName) {
		return mapDatas.get(createMapKey(appName, monName));
	}

}
